/**
 * Created by zf233 on 2020/2/10
 */
var target;
var ticket;
var randstr;

function callback(res) {
    if (res.ret === 0) {
        ticket = res.ticket;
        randstr = res.randstr;
        target.click();
    } else {
        ticket = '';
        randstr = '';
    }
}

$(function () {
    // toastr.options = {
    //     "positionClass": "toast-top-title-center",
    // };
    // toastr.info("因技术升级，不再支持用户名相关操作!!!");
    // toastr.info("数据已迁移，老用户请使用旧账号注册时使用的手机或邮箱配合验证码或密码进行登陆，QQ老用户直接点击企鹅进行授权登陆");

    $("#login_form_submit_button_before").click(function () {
        target = $("#login_form_submit_button");
        $("#TencentCaptcha").click();
    })
    $("#email_code_login_form_button_before").click(function () {
        target = $("#email_code_login_form_button");
        $("#TencentCaptcha").click();
    })
    $("#phone_code_login_form_button_before").click(function () {
        target = $("#phone_code_login_form_button");
        $("#TencentCaptcha").click();
    })
    $("#login_form").keydown(function (event) {
        if (event.keyCode === 13) {
            $("#login_form_submit_button_before").click();
            return false;
        }
    })
    $("#email_code_login_form").keydown(function (event) {
        if (event.keyCode === 13) {
            $("#email_code_login_form_button_before").click();
            return false;
        }
    })
    $("#phone_code_login_form").keydown(function (event) {
        if (event.keyCode === 13) {
            $("#phone_code_login_form_button_before").click();
            return false;
        }
    })

    // foot start
    $(".share_C").click(fun03);
    function fun03() {
        imgShow("#outerdiv", "#innerdiv", "#bigimg", "/admin/starter")
    }
    $(".share_B").click(fun02);
    function fun02() {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("注册登陆后点击用户名，选择意见反馈，填写您的联系方式和反馈内容，我会及时联系您，感谢支持！");
        // imgShow("#outerdiv", "#innerdiv", "#bigimg", "https://www.xcloud.show/static/img/xcloud_front/add_author.jpg")
    }
    $(".share_A").click(fun01);
    function fun01() {
        imgShow("#outerdiv", "#innerdiv", "#bigimg", "/admin/app")
    }
    function imgShow(outerdiv, innerdiv, bigimg, src) {
        $(bigimg).attr("src", src);
        $(bigimg).attr("src", src).load(function () {
            var windowW = $(window).width();
            var windowH = $(window).height();
            var realWidth = this.width;
            var realHeight = this.height;
            var imgWidth, imgHeight;
            var scale = 0.8;
            if (realHeight > windowH * scale) {
                imgHeight = windowH * scale;
                imgWidth = imgHeight / realHeight * realWidth;
                if (imgWidth > windowW * scale) {
                    imgWidth = windowW * scale
                }
            } else {
                if (realWidth > windowW * scale) {
                    imgWidth = windowW * scale;
                    imgHeight = imgWidth / realWidth * realHeight
                } else {
                    imgWidth = realWidth;
                    imgHeight = realHeight
                }
            }
            $(bigimg).css("width", imgWidth);
            var w = (windowW - imgWidth) / 2;
            var h = (windowH - imgHeight) / 2;
            $(innerdiv).css({ "top": h, "left": w });
            $(outerdiv).fadeIn("fast")
        });
        $(outerdiv).click(function () {
            $(this).fadeOut("fast")
        })
    }
    $("#a_learn_notes").click(function () {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("登陆以获取作者学习笔记-账号:notes 密码:notes");
    })
    $("#copyright").click(function () {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("部分图标来自iconfont。如网站图标等侵犯到您的权益,请发送邮件至 13784570379@163.com");
    })
    $("#to_contact_me").click(function () {
        toastr.options = {
            "positionClass": "toast-top-title-center",
        };
        toastr.info("QQ:1326284235; 邮箱:13784570379@163.com");
    })
    // foot end

    // input content format check start
    $("#username").focus(function () {
        $(this).css("border-color", "");
    })
    $("#password").focus(function () {
        $(this).css("border-color", "");
    })
    $("#email").focus(function () {
        $(this).css("border-color", "");
    })
    $("#phone").focus(function () {
        $(this).css("border-color", "");
    })
    $("#code").focus(function () {
        $(this).css("border-color", "");
    })
    $("#phone_code").focus(function () {
        $(this).css("border-color", "");
    })

    $("#username").blur(function () {
        var username = $(this).val().replace(/\s+/g, "");
        if (username.length > 0 && !(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(username) || /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(username))) {
            $(this).css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("手机或邮箱格式有误");
        }
    })
    $("#password").blur(function () {
        var password = $(this).val().replace(/\s+/g, "");
        if (password.length > 0 && !/^[a-zA-Z]\w{4,15}$/.test(password)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("密码格式有误:以字母开头的5位至16位字母数字下划线组合");
            $(this).css("border-color", "red");
        }
    })
    $("#email").blur(function () {
        var email = $(this).val().replace(/\s+/g, "");
        if (email.length > 0 && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
            $(this).css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("邮箱格式有误");
        }
    })
    $("#phone").blur(function () {
        var phone = $(this).val().replace(/\s+/g, "");
        if (phone.length > 0 && !/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)) {
            $(this).css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("手机格式有误");
        }
    })
    $("#code").blur(function () {
        var code = $(this).val().replace(/\s+/g, "");
        if (code.length > 0 && !/^\d{6}$/.test(code)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("验证码格式有误");
            $(this).css("border-color", "red");
        }
    })
    $("#phone_code").blur(function () {
        var code = $(this).val().replace(/\s+/g, "");
        if (code.length > 0 && !/^\d{6}$/.test(code)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("验证码格式有误");
            $(this).css("border-color", "red");
        }
    })

    $("#username").keyup(function () {
        var username = $(this).val().replace(/\s+/g, "");
        if (username.length > 0 && !(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(username) || /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(username))) {
            $(this).css("border-color", "red");
        } else {
            $(this).css("border-color", "");
        }

    })
    $("#password").keyup(function () {
        var password = $(this).val().replace(/\s+/g, "");
        if (password.length > 0 && !/^[a-zA-Z]\w{4,15}$/.test(password)) {
            $(this).css("border-color", "red");
        } else {
            $(this).css("border-color", "");
        }
    })
    $("#email").keyup(function () {
        var email = $(this).val().replace(/\s+/g, "");
        if (email.length > 0 && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
            $(this).css("border-color", "red");
        } else {
            $(this).css("border-color", "");
        }
    })
    $("#phone").keyup(function () {
        var phone = $(this).val().replace(/\s+/g, "");
        if (phone.length > 0 && !/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)) {
            $(this).css("border-color", "red");
        } else {
            $(this).css("border-color", "");
        }
    })
    $("#code").keyup(function () {
        var code = $(this).val().replace(/\s+/g, "");
        if (code.length > 0 && !/^\d{6}$/.test(code)) {
            $(this).css("border-color", "red");
        } else {
            $(this).css("border-color", "");
        }
    })
    $("#phone_code").keyup(function () {
        var code = $(this).val().replace(/\s+/g, "");
        if (code.length > 0 && !/^\d{6}$/.test(code)) {
            $(this).css("border-color", "red");
        } else {
            $(this).css("border-color", "");
        }
    })
    // input content format check end

    // login start
    $("#login_form_submit_button").click(function () {
        var username = $("#username").val().replace(/\s+/g, "");
        var password = $("#password").val().replace(/\s+/g, "");
        if (username == "") {
            $("#username").css("border-color", "red");
            return false;
        }
        if (password == "") {
            $("#password").css("border-color", "red");
            return false;
        }
        if (!(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(username) == true || /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(username) == true)) {
            $("#username").css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("手机或邮箱格式有误:以字母开头的5位至16位字母数字下划线组合");
            return false;
        }
        if (/^[a-zA-Z]\w{4,15}$/.test(password) == false) {
            $("#password").css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("密码格式有误:以字母开头的5位至16位字母数字下划线组合");
            return false;
        }
        if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(username) == true) {
            $("#username").val(username.replace(/www./, ""));
        }
        md5Password = $.md5('zf233' + password)
        var remember = $("#remember_pwd").prop("checked") ? 1 : 0;
        var data = {
            phone: username,
            password: md5Password,
            ticket: ticket,
            randstr: randstr,
            remember: remember
        }
        login('/user/login', data);
        return false;
    })

    $("#email_code_login_form_button").click(function () {
        var email = $("#email").val().replace(/\s+/g, "");
        var code = $("#code").val().replace(/\s+/g, "");
        if (email == "") {
            $("#email").css("border-color", "red");
            return false;
        }
        if (code == "") {
            $("#code").css("border-color", "red");
            return false;
        }
        if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
            $("#email").css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("邮箱格式有误");
            return false;
        }
        if (!/^\d{6}$/.test(code)) {
            $("#code").css("border-color", "red");
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("验证码格式有误");
            return false;
        }
        email = email.replace(/www./, "");
        var remember = $("#remember_email").prop("checked") ? 1 : 0;
        var data = {
            email: email,
            code: code,
            ticket: ticket,
            randstr: randstr,
            remember: remember
        }
        login('/user/login/email', data);
        return false;
    })

    $("#send_code").click(function () {
        var email = $("#email").val().replace(/\s+/g, "");
        if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
            email = email.replace(/www./, "");
            $.ajax({
                async: true,
                url: "/user/login/code/email",
                data: {
                    email: email
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        timeout(data.data, $("#send_code"));
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        $("#code").focus();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                        $("#email").val("");
                    }
                },
                error: function (data) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("发送失败");
                    $("#email").val("");
                },
                type: "post"
            });
        } else {
            if (email.length > 0) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("邮箱格式有误");
                $("#email").css("border-color", "red");
            } else {
                $("#email").css("border-color", "red");
            }
        }
    })
    var countdown;
    function timeout(timeout, target) {
        var count = timeout;
        clearInterval(countdown);
        countdown = setInterval(CountDown, 1000);

        function CountDown() {
            $(target).val(count);
            $(target).css("color", "");
            if (count == 0) {
                $(target).val("发送");
                clearInterval(countdown);
            }
            count--;
        }
    }
    // login end

    // login regist form show or hide start
    $("#open_login_form_a").click(function () {
        $("#login_form").show();
    })

    $("#phone_code_login").click(function () {
        $("#phone_code_login_form").show();
        $("#login_form").hide();
    })

    $("#close_login_form_a").click(function () {
        $("#login_form").hide();
    })

    $("#go_email_code_code_a").click(function () {
        $("#login_form").hide();
        $("#email_code_login_form").show();
    })

    $(".go_login_a").click(function () {
        $("#email_code_login_form").hide();
        $("#phone_code_login_form").hide();
        $("#login_form").show();
    })
    // login regist form show or hide end

    $("#phone_code_login_form_button").click(function () {
        var phone = $("#phone").val().replace(/\s+/g, "");
        var phoneCode = $("#phone_code").val().replace(/\s+/g, "");
        var phoneTest = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        var phoneCodeTest = /^\d{6}$/;
        if (phone == "") {
            $("#phone").css("border-color", "red");
            return false;
        }
        if (phoneCode == "") {
            $("#phone_code").css("border-color", "red");
            return false;
        }
        if (!phoneTest.test(phone)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("手机号码格式有误");
            $("#phone").css("border-color", "red");
            return false;
        }
        if (!phoneCodeTest.test(phoneCode)) {
            toastr.options = {
                "positionClass": "toast-top-title-center",
            };
            toastr.error("验证码格式有误");
            $("#phone_code").css("border-color", "red");
            return false;
        }
        var remember = $("#remember_phone").prop("checked") ? 1 : 0;
        var data = {
            phone: phone,
            code: phoneCode,
            ticket: ticket,
            randstr: randstr,
            remember: remember
        }
        login('/user/login/phone', data);
        return false;
    })
    function login(url, data) {
        $.ajax({
            async: true,
            url: url,
            data: data,
            dataType: "json",
            success: function (data) {
                if (data.status == 200) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.info(data.msg);
                    setTimeout(function () {
                        document.location = '/user/jump/home';
                    }, 800);
                } else {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error(data.msg);
                }
            },
            error: function (data) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("登陆失败");
            },
            type: "post"
        });
    }

    $("#send_code_phone").click(function () {
        var phone = $("#phone").val().replace(/\s+/g, "");
        var phoneTest = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        if (phoneTest.test(phone)) {
            $.ajax({
                async: true,
                url: "/user/login/code/phone",
                data: {
                    phone: phone
                },
                dataType: "json",
                success: function (data) {
                    if (data.status == 200) {
                        timeout(data.data, $("#send_code_phone"));
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.info(data.msg);
                        $("#phone_code").focus();
                    } else {
                        toastr.options = {
                            "positionClass": "toast-top-title-center",
                        };
                        toastr.error(data.msg);
                        $("#phone").val("");
                    }
                },
                error: function (data) {
                    toastr.options = {
                        "positionClass": "toast-top-title-center",
                    };
                    toastr.error("发送失败");
                    $("#phone").val("");
                },
                type: "post"
            });
        } else {
            if (phone.length > 0) {
                toastr.options = {
                    "positionClass": "toast-top-title-center",
                };
                toastr.error("手机格式有误");
                $("#phone").css("border-color", "red");
            } else {
                $("#phone").css("border-color", "red");
            }
        }
    })
});